import 'package:flutter/material.dart';
import 'package:fzcgx_app/common/adapter_utils.dart';
import 'package:fzcgx_app/provider/provider_widget.dart';
import 'package:fzcgx_app/ui/base/base_view_model.dart';
import 'package:fzcgx_app/ui/order/view_model/order_add_colour_view_model.dart';
import 'package:fzcgx_app/ui/widget/custom_appbar_widget.dart';

class OrderAddColourPage extends StatefulWidget {
  final Function callback;
  OrderAddColourPage({Key key,this.callback}) : super(key: key);

  @override
  _OrderAddColourPageState createState() => _OrderAddColourPageState();
}

class _OrderAddColourPageState extends State<OrderAddColourPage> {
  @override
  Widget build(BuildContext context) {
    return ProviderWidget<OrderAddColourViewModel>(
      onReady: (viewModel) {
        viewModel.requestApi();
      },
      model: OrderAddColourViewModel(),
      builder: (context, viewModel, child) {
        return viewModel.state == ViewState.Success
            ? Scaffold(
                backgroundColor: Colors.white,
                appBar: CustomAppbar(
                  title: '添加颜色',
                  actions: <Widget>[
                    InkWell(
                        onTap: () {
                          viewModel.showAlert(context);
                        },
                        child: Container(
                            alignment: Alignment.center,
                            padding: EdgeInsets.only(
                                right: AdapterUtils.getWidth(15)),
                            child: Text(
                              '新增颜色',
                              style: TextStyle(
                                  color: Color(0xff15A1FF),
                                  fontWeight: FontWeight.w500,
                                  decoration: TextDecoration.none,
                                  fontSize: AdapterUtils.getSp(16)),
                            )))
                  ],
                ),
                body: SafeArea(child: _buildSuccessWidget(viewModel)),
                bottomNavigationBar: BottomAppBar(
                  child: InkWell(
                    onTap: () {
                      viewModel.selectPost(widget.callback);
                    },
                    child: Container(
                      height: AdapterUtils.getHeight(84),
                      width: AdapterUtils.screenWidth(),
                      color: Color(0xff15A1FF),
                      alignment: Alignment.center,
                      child: Text(
                        '确认添加',
                        style: TextStyle(
                          fontSize: AdapterUtils.getSp(18),
                          color: Colors.white,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                    ),
                  ),
                ),
              )
            : Container(
                color: Colors.white,
              );
      },
    );
  }

  Widget _buildSuccessWidget(OrderAddColourViewModel viewModel) {
    return SingleChildScrollView(
        child: Column(children: <Widget>[
      SizedBox(
        height: AdapterUtils.getHeight(4),
      ),
      Container(
        height: AdapterUtils.getHeight(38),
        color: Color(0xffEAF7FE),
        child: Center(
          child: Text(
            '默认颜色不可删除，点击添加可新增颜色',
            style: TextStyle(
              fontSize: AdapterUtils.getSp(13),
              color: Color(0xff15A1FF),
              fontWeight: FontWeight.w500,
            ),
          ),
        ),
      ),
      ListView.builder(
          shrinkWrap: true,
          primary: false,
          itemCount: viewModel.models.length,
          itemBuilder: (BuildContext context, int index) {
            return InkWell(
              onTap: () {
                viewModel.selectModel(viewModel.models[index]);
              },
              child: Container(
                  padding: EdgeInsets.only(
                      left: AdapterUtils.getWidth(24),
                      right: AdapterUtils.getWidth(14)),
                  height: AdapterUtils.getHeight(70),
                  color: viewModel.models[index].check
                      ? Color(0xffF0FAFE)
                      : Colors.white,
                  child: Row(
                    children: <Widget>[
                      Text(
                        viewModel.models[index].name,
                        style: TextStyle(
                          fontSize: AdapterUtils.getSp(16),
                          color: Color(0xff3B4859),
                          fontWeight: FontWeight.w600,
                        ),
                      ),
                      Spacer(),
                      Container(
                        width: AdapterUtils.getWidth(16),
                        height: AdapterUtils.getHeight(16),
                        decoration: BoxDecoration(
                          border: viewModel.models[index].check
                              ? Border()
                              : Border.all(color: Color(0xffD7D8DB), width: 2),
                          borderRadius: BorderRadius.all(Radius.circular(8)),
                          color: viewModel.models[index].check
                              ? Color(0xff15A1FF)
                              : Colors.white,
                        ),
                      )
                    ],
                  )),
            );
          })
    ]));
  }
}
